$(function () {
	//系统管理
	//----用户管理
	/**
     * Init
     *
     * 
     * |-- 
     * |   `-- 
     * |   `-- 
     * |	   `-- 
     * |		   `-- 
     * |		   `-- 
     * |			   `-- 
     * |		   `-- 
     * |-- 
     * |-- 
     */
	var ad = appData();
	
	var tmplObj = null;
	var arrys = []//删除的数组
	console.log('已加载')
	
	DomNameStart()
	function DomNameStart() {
		
		  $("#search2").on("click",function(){
		    $(".advanced-search").toggle(200);
		  })  
		//侧面添加class、索引加载
		$('.sys-sidebar ul li').eq(0).find('ul li ').eq(0).addClass('active')
		
		var name = $('.sys-sidebar').find('li .active a').text()
		
		$("#TopName").text(name)//替换头部日志名称
		
		
		loadDom('USER_MAN_ROLE',$("#role"),"角色")
		

		function loadDom(api,id,val) {
			ad.selectList(api, {},function(data) {
				if(val==''){
					$(id).empty()
				}else{
					$(id).empty().append("<option value='-1'> "+val+"</option>")
				}
				for(var i=0;i<data.length;i++){
					$(id).append("<option value='"+data[i].id+"'>"+data[i].value+"</option>")
				}
				
				
			})
		}
		
		
				
		
			showTable(true, 1)
			
		
		$("#delList").on('click',function(){
			if(arrys.length==0){
				_as.jAlert("请选择删除的序列")
			}else{
				ad.selectList("USER_DEL_LIST",{del:arrys},function(){
					_as.jAlert("<p>删除成功</p>")
					showTable(true,1)
					arrys = []
				})
			}
		})
		
		
		
	}
	
	
	function domCase() {
		$("#search1").on('click',function(){
			var searchVal  = $("#searchVal").val() 
			
			showTable(true,1)
		})		
		
		

		
		
		

		
	}
	function addDomName(tips) {
			 
		
				
				if(arrys.indexOf(tips)==-1){
					arrys.push(tips)
				}else{
					var num =  arrys.indexOf(tips);
	    				arrys.splice(num,1);
				}
				console.log(arrys)
			
	}
	var power = false
	function addDomName2() {
		arrys = []
		power = !power
		if(power){
			for(var i=0;i<$("#tableBody tr").length;i++){
				arrys.push($("#tableBody tr:eq("+i+")").attr('data-id'))
				//console.log($("#tableBody tr").attr('data-id'))
			}
		}else{
			arrys = []
		}
		
			console.log(arrys)
			
	}
	//表格show加载
	function showTable(dontRenderPager, page) {
  		
		var params = {
			"page":page,
//			"top1":top1,
//			"top2":top2,
			"pageSize": appApi('PAGE_SIZE')
		}
  		
		ad.insert("USER_MAN_LIST", params, function(data) {
			
			$("#tableBody").empty();
			if (data.length === 0) {
				$("#tableBody").append("<tr><td colspan='3' class='colspanCss'><h1>暂无数据</h1></td></tr>");
				return false;
			}
			
			
			
			$.get('scripts/app/safe/sysManage/UserList.tmpl', function(tmpl) {
			
				
				$("#tableBody").empty().append($.tmpl(tmpl, data))
					
					
					
				if ($("input[type='checkbox'],input[type='radio']").length > 0) {
                    $("input[type='checkbox'],input[type='radio']").not(".toggle input[type='checkbox']").iCheck({
                        checkboxClass: 'icheckbox',
                        radioClass: 'iradio',
                    }).on('ifClicked', function (event) {
                        var id = $(event.target).parents('tr').attr('data-id')
                        var allId = $(event.target).attr('id')
                        //全选与反选
                        if(!allId){
                        	addDomName(id)
                        }else{
                        	addDomName2()
                        }
                    	
                        if(allId=='allCheck'){
                        	
                           if(!$(event.target).prop('checked')){
                           	
                               $('.A1').iCheck('check')
                           }else{
                               $('.A1').iCheck('uncheck')
                           }
                        }
                    });
                }
				
				
                if (dontRenderPager) {
                    renderPager(data.totalRecord);
                }
                
                
			})
		})
	}
	function renderPager(totalRecord, nowPage) {
		
		$("div.page").pagination(totalRecord, appApi("PAGE_SIZE"), {
			
			callback: function(page_id, jq) {
				pageNumNow1 = page_id + 1;
				showTable(false, pageNumNow1);
				arrys = [];
                power = false
				$("#allCheck").iCheck('uncheck')
			}
		}, nowPage);
	};
	
	
	
 

	

})